
require"script/ui"
require"script/markov"

markov = Markov:new(3, 4)

rps_ui = UI:new();
rps_array = {"paper","rock","scissors"};
rps_text = {};
timer = .0;
switch_time = .1;
cpu_rps = rps_array[1];
player_rps = nil;
nt = .0;
next_time = 1.0;
rps_result = 0;
rps_result_state = {[0]=0,[1]=0,[-1]=0};

function paper_callback()
	process(1);
end

function rock_callback()
	process(2);
end

function scissors_callback()
	process(3);
end

function process(rps)

	player_rps = rps_array[rps];
	rc = markov:predict();

	if rc.total==0 then
		cpu_rps = rps_array[math.random(1,3)];
	else
		local p = math.random(0,99)/100;
		if p<rc[1] then
			cpu_rps = rps_array[3];
		elseif p<rc[2] then
			cpu_rps = rps_array[1];
		else
			cpu_rps = rps_array[2];
		end
	end

--~ 	local temp_str = "";
--~ 	for i,v in pairs(markov.history) do
--~ 		temp_str = temp_str.."->"..rps_array[v];
--~ 	end
--~ 	print(temp_str);

--~ 	temp_str = "next:";
--~ 	for i=1,3 do
--~ 		temp_str = temp_str..rps_array[i]..":"..(rc[i]*100).."%";
--~ 	end
--~ 	print(temp_str);

	if cpu_rps=="paper" and player_rps=="scissors" then
		rps_result = 1;
	elseif cpu_rps=="rock" and player_rps=="paper" then
		rps_result = 1;
	elseif cpu_rps=="scissors" and player_rps=="rock" then
		rps_result = 1;
	elseif cpu_rps==player_rps then
		rps_result = 0;
	else
		rps_result = -1;
	end
	markov:push(rps);

	rps_result_state[rps_result] = rps_result_state[rps_result]+1;
	rps_ui.control["result"].text = "result : win("..rps_result_state[1]
		.."), lose("..rps_result_state[-1].."), draw("..rps_result_state[0]..")";
end

function love.load()
	math.randomseed(os.time());

	love.graphics.setBackgroundColor({255,255,255});

	Fonts:create("normal", 24);

	Images:create("paper", "res/paper.png");
	Images:create("rock", "res/rock.png");
	Images:create("scissors", "res/scissors.png");

	rps_ui.control["result"] = StaticText:new("result : win(0), lose(0), draw(0)", "normal", 0, 0);

	rps_ui.control["CPU"] = StaticText:new("CPU", "normal", 200, 50);
	rps_ui.control["?"] = StaticText:new("?", "normal", 240, 100);

	rps_text[1] = StaticText:new("WIN", "normal", 220, 160);
	rps_text[1].color = {0,0,255};
	rps_text[-1] = StaticText:new("LOSE", "normal", 220, 160);
	rps_text[-1].color = {255,0,0};
	rps_text[0] = StaticText:new("DRAW", "normal", 220, 160);

	rps_ui.control["paper"] = ImageBtn:new("paper", 50, 350);
	rps_ui.control["rock"] = ImageBtn:new("rock", 200, 350);
	rps_ui.control["scissors"] = ImageBtn:new("scissors", 350, 350);

	rps_ui.control["paper"].mousereleased_callback = paper_callback;
	rps_ui.control["rock"].mousereleased_callback = rock_callback;
	rps_ui.control["scissors"].mousereleased_callback = scissors_callback;

	rps_ui.control["paper"].normal_color = {125,125,125};
	rps_ui.control["rock"].normal_color = {125,125,125};
	rps_ui.control["scissors"].normal_color = {125,125,125};

	rps_ui.control["paper"].hover_color = {200,200,200};
	rps_ui.control["rock"].hover_color = {200,200,200};
	rps_ui.control["scissors"].hover_color = {200,200,200};
end

function love.update(dt)
	if player_rps==nil then
		timer = timer+dt;
		if timer>switch_time then
			timer = .0;
			cpu_rps = rps_array[math.random(1,3)];
		end

		rps_ui.control["CPU"]:show();
		rps_ui.control["?"]:show();
	else
		rps_ui.control["CPU"]:hide();
		rps_ui.control["?"]:hide();

		nt = nt+dt;
		if nt>next_time then
			nt = 0;
			player_rps = nil;
		end
	end
	rps_ui:update(dt);
end

function love.draw()
	Images:draw(cpu_rps,200,50);
	if player_rps~=nil then
		Images:draw(player_rps,200,200);
		rps_text[rps_result]:draw();
	end
	rps_ui:draw();
end

function love.mousepressed(x, y, button)
	rps_ui:mousepressed(x, y, button);
end

function love.mousereleased(x, y, button)
	rps_ui:mousereleased(x, y, button);
end
